package cz.vutbr.fit.xzemko01.tfmcbs.validation.validators;

import java.util.Map;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;

import cz.vutbr.fit.xzemko01.tfmcbs.model.Component;
import cz.vutbr.fit.xzemko01.tfmcbs.model.Port;
import cz.vutbr.fit.xzemko01.tfmcbs.validation.utils.DiagnosticBuilder;

public class PortValidator implements Validator<Port> {
	
	private static Validator<Port> portValidator;
	
	private PortValidator() {
	}
	
	public static Validator<Port> getInstance() {
		if(portValidator == null) {
			portValidator = new PortValidator();
		}
		return portValidator;
	}
	
	@Override
	public boolean validate(Port port, DiagnosticChain diagnostics, Map<Object, Object> context) {
		return validatePortHasName(port, diagnostics, context);
	}
	
	private boolean validatePortHasName(Port port, DiagnosticChain diagnostics, Map<Object, Object> context) {
		String name = port.getName();
		boolean result = true;
		
		if(name == null || name.trim().isEmpty()) {
			String stringInComponent = ""; 
			Component component = port.getComponent();
			if(component != null) {
				stringInComponent = " in component \"" + component.getName() + "\"";
			}
			
			Diagnostic diagnostic = new DiagnosticBuilder(port)
				.setMessage("Port name is empty" + stringInComponent + ".").build();
			diagnostics.add(diagnostic);
			result = false;
		}
		return result;
	}
}
